package edu.vt.ece.rt.benchmark;

import java.io.File;
import java.io.IOException;

import edu.vt.ece.rt.QR;
import edu.vt.ece.rt.dstm.TMProxy;
import edu.vt.ece.rt.quorum.ClustersManager;
import edu.vt.ece.rt.quorum.util.Names;
import edu.vt.ece.rt.quorum.util.NodeReaper;
import edu.vt.ece.rt.util.Logger.FileLogger;
import edu.vt.ece.rt.util.Logger.Logger;

/*
 * BenchMark Entry Point
 * @author Sudhanshu
 */

public class Application {
	public static void main(String[] args) throws Throwable {

		initParameters(args);
		QR.init();		
		TMProxy.start();
		
		System.out.println("Running Bench Marks...");
		
		
		String benchmarkApplication = System.getProperty(Names.benchmark);
		Benchmark benchmark = (Benchmark)Class.forName("edu.vt.ece.rt.benchmark." + benchmarkApplication + ".Benchmark").newInstance();

		Logger.debug("Starting benchmark execution for "+benchmarkApplication);
		
		benchmark.start();
		System.out.println("Finished Bench Marks...");
		QR.close();
		System.out.println("Chanels closed");
		System.out.println("Ending this node");
	}
	
	static void initParameters(String [] args){
		if (args.length > 0 && args[0].equals("default")) {
			QR.script = 0;
		} else {
			QR.script = 1;
		}
		
		File logDir;
		try {
			logDir = FileLogger.init(System.getProperty(Names.benchmark)+"-QR", Integer.toString(Integer.getInteger("nodeId")));
		
		if(Boolean.getBoolean(Names.debug))
			Logger.redirect(logDir, Integer.toString(Integer.getInteger("nodeId"))	);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		ClustersManager.baseClusterId = Integer.getInteger(Names.nodeId);	
	}
}
